package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import model.Student;
import util.JDBCUtil;
import util.NumberUtil;

/**
 * Servlet implementation class TransitionServlet
 */
@WebServlet("/TransitionServlet")
public class TransitionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * 过渡类，获取学生信息并保存
     * @see HttpServlet#HttpServlet()
     */
    public TransitionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		String sql;
		String info = request.getParameter("info");
		info = new String(info.getBytes("ISO-8859-1"), "utf-8"); //中文不乱码
		double[] averScore = (double[]) this.getServletContext().getAttribute("averScore");
		if(NumberUtil.isNumber(info)) {
			sql = "select * from study where id = "+info;
		}else {
			sql = "select * from study where name = "+"'"+info+"'";
		}
		Connection con = null;
		PreparedStatement pStatement = null;
		Statement stmt = null;
		ResultSet rs = null;
		boolean flag = false;
		try{
			con = JDBCUtil.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			Student stu = new Student();
			while(rs.next()) {
				flag = true;
				stu.setId(rs.getString(1));
				stu.setName(rs.getString(2));
				stu.setSex(rs.getString(3));
				stu.setClassNo(rs.getString(4));
				stu.setMajor(rs.getString(5));
				stu.setFaculty(rs.getString(6));
				stu.setMidterm(rs.getString(7));
				stu.setTerminal(rs.getString(8));
				stu.setPta(rs.getString(9));
				stu.setMOOC(rs.getString(10));
				stu.setFinalScore(rs.getString(11));
			}
			if(!flag) {
				request.getRequestDispatcher("NotFind.jsp").forward(request, response);
			}
			double[] score = {Double.parseDouble(stu.getMidterm()),Double.parseDouble(stu.getTerminal()),Double.parseDouble(stu.getPta()),Double.parseDouble(stu.getMOOC()),Double.parseDouble(stu.getFinalScore())};
			String[] exam = {"期中成绩","期末成绩","pta成绩","MOOC成绩","最终成绩"};
		//	Map<String, Object> map = new HashMap<>();
			this.getServletContext().setAttribute("info", info); //保存信息
			this.getServletContext().setAttribute("score",score);
			this.getServletContext().setAttribute("exam",exam);
			this.getServletContext().setAttribute("averScore",averScore);
		//	request.setAttribute("averScore", averScore);
		/*	map.put("score",score);
	        map.put("exam",exam);
	        map.put("averScore", averScore);
	        response.getWriter().println(JSON.toJSONString(map));*/
	        request.getRequestDispatcher("StuInfo.jsp").forward(request, response); //转发给StuInfo
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.realeaseAll(rs, stmt, con, pStatement);
		}
	}

}
